package com.mc.lp.database.service.handler;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.util.CollectionUtils;

import com.mc.lp.database.service.handler.constant.FormHandler;
import com.mc.lp.database.service.handler.constant.FormRequestType;
import com.mc.lp.database.service.handler.constant.IFormHandler;
import com.mc.lp.service.MicroCampusAbstractService;
import com.mc.lp.shared.vo.IValueObject;

public class FormHandlerFactory extends MicroCampusAbstractService<FormHandlerFactory> {
	
	private ConcurrentHashMap<String, ArrayList<IFormHandler<IValueObject, IValueObject>>> handlerMap;
	
	public IFormHandler<IValueObject, IValueObject> getHandler(FormHandler handler,FormRequestType requestType){
		ArrayList<IFormHandler<IValueObject, IValueObject>> handlerList=handlerMap.get(handler.getName());
		if(CollectionUtils.isEmpty(handlerList)){
			throw new RuntimeException("No Handler Found for this request");
		}else{
			//check if handlers are  not empty and available.
			if(!CollectionUtils.isEmpty(handlerList)){
				for(IFormHandler<IValueObject, IValueObject> formHandler : handlerList){
					if(formHandler.getRequestHandlingType().contains(requestType))
						return formHandler;
				}
			}
		}
		throw new RuntimeException("No Handler found to entertain this request");
	}

	public ConcurrentHashMap<String, ArrayList<IFormHandler<IValueObject, IValueObject>>> getHandlerMap() {
		return handlerMap;
	}

	public void setHandlerMap(ConcurrentHashMap<String, ArrayList<IFormHandler<IValueObject, IValueObject>>> handlerMap) {
		this.handlerMap = handlerMap;
	}
	
}
